package com.wang.cloud.common.swagger.customizers;

import com.wang.cloud.common.core.enums.BaseEnum;
import com.wang.cloud.common.core.validator.CheckEnum;
import com.wang.cloud.common.swagger.annotations.SchemaEnum;
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.oas.models.media.Schema;
import org.springdoc.core.customizers.PropertyCustomizer;

import java.lang.annotation.Annotation;

/**
 * 自定义枚举类文档
 *
 * @Author 1024创新实验室-主任:卓大
 * @Date 2023/12/25 23:28:51
 * @Wechat zhuoda1024
 * @Email lab1024@163.com
 * @Copyright <a href="https://1024lab.net">1024创新实验室</a>
 */
public class SchemaEnumPropertyCustomizer implements PropertyCustomizer {

    @Override
    public Schema customize(Schema schema, AnnotatedType type) {
        Annotation[] ctxAnnotations = type.getCtxAnnotations();
        if (ctxAnnotations == null) {
            return schema;
        }

        StringBuilder description = new StringBuilder();
        for (Annotation ctxAnnotation : ctxAnnotations) {
            if (ctxAnnotation.annotationType()
                    .equals(io.swagger.v3.oas.annotations.media.Schema.class)) {
                description.append(((io.swagger.v3.oas.annotations.media.Schema) ctxAnnotation).description());
                break;
            }
        }

        for (Annotation ctxAnnotation : ctxAnnotations) {
            if (ctxAnnotation.annotationType()
                    .equals(CheckEnum.class) && ((CheckEnum) ctxAnnotation).required()) {
                description.append("<font style=\"color: red;\">【必填】</font>");
                break;
            }
        }

        for (Annotation ctxAnnotation : ctxAnnotations) {
            if (ctxAnnotation.annotationType()
                    .equals(SchemaEnum.class)) {
                description.append(((SchemaEnum) ctxAnnotation).desc());
                Class<? extends BaseEnum> clazz = ((SchemaEnum) ctxAnnotation).value();
                description.append(BaseEnum.getInfo(clazz));
                break;
            }
        }

        if (description.length() > 0) {
            schema.setDescription(description.toString());
        }
        return schema;
    }

}
